﻿$.extend({

    UserManagementSiteCollUsers: {

        LoadSiteCollectionsDropDown: function () {

            $('#ddlSiteCollections').empty();
            $('#ddlSiteCollections').append($('<option></option>').val("").text("-- Select --"));

            $.each(asites, function (val, text) {
                $('#ddlSiteCollections').append($('<option></option>').val(text["Url"]).text(text["Name"]));
            });
        },

        LoadPage: function () {

            var jqxhr = $.get('html/ManageForm.html');
            jqxhr.success(function (data) {

                $('#ManageFormDialog').html(data);

                // on Click event for adding/removing groups - Site Collection User Management
                $('#btnAddItems').click(function () {
                    $("#ManageFormTable").hide();
                    $("#LoadingImage").show();
                    $.UserManagementSiteCollUsers.AddGroupsToUser($('#lblEntityName').html());
                    $("#LoadingImage").hide();
                    $("#ManageFormTable").show();
                });

                $('#btnRemoveItems').click(function () {
                    $("#ManageFormTable").hide();
                    $("#LoadingImage").show();
                    $.UserManagementSiteCollUsers.RemoveGroupsFromUser($('#lblEntityName').html());
                    $("#LoadingImage").hide();
                    $("#ManageFormTable").show();
                });

                $.UserManagement.SetButtonStyle();
                $.UserManagement.SetGlobalClickEvents();
            });

            jqxhr.error(function () {
                alert('Failed to load edit form.');
            });
        },

        PopulateDataTable: function () {

            try {
                $.UserManagementSiteCollUsers.LoadPage();

                // Initialize Group(s) Data Table
                var arryUsers = $.UserManagementDA.GetAllUsers();
                var arryUniqueUser = $.UMUtilities.SortUnique(arryUsers, "UserName");
                var dt = $('#siteCollUserListTable').DataTable();
                dt.clear().rows.add(arryUsers).draw();

                // Wire up the data table to handle row selection
                $("#siteCollUserListTable tbody").delegate("tr", "click", function () {
                    var userName = $("td:first", this).text();
                    var loginName = $("td:eq(1)", this).text();
                    $.UserManagementSiteCollUsers.PopulateFormPage(userName, loginName);
                });

                $("#siteCollUserListTable td:first-child").addClass("nowrap");
            }
            catch (e) {
                alert(e);
            }
        },

        PopulateFormPage: function (userName, loginName) {

            try {
                var arryUserInfo = $.UserManagementDA.GetUserInfo(loginName);
                $.UserManagementSiteCollUsers.LoadSiteCollectionsDropDown();

                $('#ddlSiteCollections').change(function () {
                    if ($('#ddlSiteCollections').val() != "") {
                        $.UserManagementSiteCollUsers.RefereshGroupLists(loginName, $('#ddlSiteCollections').val());
                    }
                    else {
                        $.UserManagement.ClearListBoxItems();
                    }
                });

                var userInfoUrl = $().SPServices.SPGetCurrentSite() + "/_layouts/userdisp.aspx?ID=" + arryUserInfo[0].UserId;

                $('#EntityTypeTitle').text("User Name:");
                $('#lblInstruction').text(instructions[1].Text);
                $('#AvailableListBoxTitle').text("Available Groups");
                $('#AssignedistBoxTitle').text("Selected Groups");

                $("#lblEntityName").text(userName);
                $("#lblEntityUrlName").text(loginName);
                $("#lblEntityUrlName").attr("href", userInfoUrl);
                $('#GroupDescription').hide();
                $('#SiteCollectionSelection').show();
                $('#ManageFormDialog').dialog('open');
            }
            catch (e) {
                alert(e);
            }
        },

        RefereshGroupLists: function (loginName, siteUrl) {

            $("#ManageFormTable").hide();
            $("#LoadingImage").show();

            $.UserManagement.ClearListBoxItems();
            $('#filterAvailableItems').val('Search for Users...');
            $('#filterSelectedItems').val('Search for Users...');

            var arryAllAvailableGrps = $.UserManagementDA.GetAllGroups(siteUrl);
            var arrySelectedGroups = $.UserManagementDA.GetGroupsByUser(loginName, siteUrl);
            var arryFilteredGrps = $.UserManagement.FilterAvailableGroups(arryAllAvailableGrps, arrySelectedGroups);

            $.UserManagement.PopulateListBox("#lstBxAvailableItems", "GroupName", arryFilteredGrps);
            $.UserManagement.PopulateListBox("#lstBxSelectedItems", "GroupName", arrySelectedGroups);

            $('#lstBxAvailableItems').filterByText($('#filterAvailableItems'), false);
            $('#lstBxSelectedItems').filterByText($('#filterSelectedItems'), false);

            $.UMUtilities.SetListBoxHeight("#lstBxSelectedItems");
            $.UMUtilities.SetListBoxHeight("#lstBxAvailableItems");

            $("#LoadingImage").hide();
            $("#ManageFormTable").show();
        },

        AddGroupsToUser: function (loginName) {

            if (loginName == null) {
                alert("No user selected.");
                return;
            }

            if ($("#lstBxAvailableItems").val() == null) {
                alert("You haven't selected any groups to add.");
                return;
            }
            else {
                $('#lstBxAvailableItems option:selected').each(function () {
                    $.UserManagementDA.AddUserToGroup($(this).val(), loginName, $('#ddlSiteCollections').val());
                    $('#lstBxSelectedItems').append($('<option></option>').val($.UMUtilities.DecodeText($(this).val())).text($.UMUtilities.DecodeText($(this).val())));
                });

                $("#lstBxAvailableItems option:selected").remove();
                $.UMUtilities.SetListBoxHeight("#lstBxSelectedItems");
                $.UMUtilities.SetListBoxHeight("#lstBxAvailableItems");
            }
        },

        RemoveGroupsFromUser: function (loginName) {

            if (loginName == null) {
                alert("No user selected.");
                return;
            }

            if ($("#lstBxSelectedItems").val() == null) {
                alert("You haven't selected any groups to remove.");
                return;
            }
            else {
                $('#lstBxSelectedItems option:selected').each(function () {
                    $.UserManagementDA.RemoveUserFromGroup($(this).val(), loginName, $('#ddlSiteCollections').val());
                    $('#lstBxAvailableItems').append($('<option></option>').val($.UMUtilities.DecodeText($(this).val())).text($.UMUtilities.DecodeText($(this).val())));
                });

                $("#lstBxSelectedItems option:selected").remove();
                $.UMUtilities.SetListBoxHeight("#lstBxSelectedItems");
                $.UMUtilities.SetListBoxHeight("#lstBxAvailableItems");
            }
        }
    }
});